﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Product.Domain.Entity
{
    public class Spu : BaseAggregationRoot
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string SpuName { get; set; }

        /// <summary>
        /// 图片
        /// </summary>
        public string PicUrl { get; set; }

        /// <summary>
        /// 分类Id
        /// </summary>
        public string CategoryId { get; set; }

        /// <summary>
        /// 品牌Id
        /// </summary>
        public string BrandId { get; set; }

        /// <summary>
        /// 销售状态
        /// </summary>
        public SalesStatus SalesStatus { get; set; }

        /// <summary>
        /// Specification
        /// </summary>
        public List<Specification> SpecificationList { get; set; }

        /// <summary>
        /// Sku
        /// </summary>
        public List<Sku> SkuList { get; set; }

        public Spu()
        {

        }

        public Spu(string spuName, string picUrl, string categoryId, string brandId, List<Specification> specificationList)
        {
            VerificationPara(spuName, picUrl, categoryId, brandId, specificationList);
            SpuName = spuName;
            PicUrl = picUrl;
            CategoryId = categoryId;
            BrandId = brandId;
            SalesStatus = SalesStatus.OnShelf;
            var specifications = new List<Specification>();
            foreach (var item in specificationList)
            {
                var specification = new Specification(Id, item.SpecificationName, item.SpecificationValue, item.Sort);
                specifications.Add(specification);
            }
            SpecificationList = specifications;
        }

        public void AddSku(List<Sku> skuList) 
        {
            this.SkuList = skuList;
        }

        /// <summary>
        /// 验证参数
        /// </summary>
        private void VerificationPara(string spuName, string picUrl, string categoryId, string brandId, List<Specification> specificationList)
        {         
            if (string.IsNullOrWhiteSpace(spuName))
            {
                throw new DomainException("Spu名称不能为空！");
            }
            if (string.IsNullOrWhiteSpace(picUrl))
            {
                throw new DomainException("请上传图片！");
            }
            if (string.IsNullOrWhiteSpace(categoryId))
            {
                throw new DomainException("请选择分类！");
            }
            if (string.IsNullOrWhiteSpace(brandId))
            {
                throw new DomainException("请选择品牌！");
            }
            if (!specificationList.Any())
            {
                throw new DomainException("规格不能为空！");
            }           
        }

        /// <summary>
        /// 添加Spu
        /// </summary>
        /// <returns></returns>
        public static Spu AddSpu(string spuName, string picUrl, string categoryId, string brandId, List<Specification> specificationList)
        {
            var spu = new Spu(spuName, picUrl, categoryId, brandId, specificationList);
            return spu;
        }

    }
}
